package com.hannesdorfmann.mosby3.sample.mvi.view.category;

import com.hannesdorfmann.mosby3.sample.mvi.businesslogic.model.Product;
import com.hannesdorfmann.mosby3.sample.mvi.view.ui.viewholder.ProductViewHolder;
import ohos.agp.components.*;
import ohos.app.Context;

import java.util.List;

public class CategoryAdapter extends BaseItemProvider {

    private Context context;
    private final LayoutScatter inflater;
    private final ProductViewHolder.ProductClickedListener productClickedListener;
    private List<Product> products;

    public CategoryAdapter(LayoutScatter inflater, ProductViewHolder.ProductClickedListener productClickedListener) {
        this.inflater = inflater;
        this.productClickedListener = productClickedListener;
    }

    public void setProducts(List<Product> products) {
        this.products = products;
    }
    @Override
    public int getCount() {
        return products == null?0:products.size();
    }

    @Override
    public Object getItem(int i) {
        return products == null?null:products.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        ProductViewHolder mProductViewHolder = ProductViewHolder.create(inflater, productClickedListener);
        Component itemComponent = mProductViewHolder.getItemView();
        if(products != null && products.size()>0)
            mProductViewHolder.bind(products.get(i));
        return itemComponent;
    }
}
